package com.study.excel.easyexcel.service;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.study.excel.easyexcel.model.ProductExcelModel;
import com.study.modules.saas.repository.dao.ProductDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;

/**
 * @Author hupengming
 * @ClassName
 * @Date 2022-11-01 11:04
 * @Version 1.0
 */
@Slf4j
@Service
public class ProductExcelService {

    @Autowired
    ProductDao productDao;

    public void productExcel(InputStream file){
        ImportParams params = new ImportParams();
        params.setNeedVerify(Boolean.TRUE);
        params.setHeadRows(1);
        try{
            ExcelImportResult<ProductExcelModel> result = ExcelImportUtil.importExcelMore(file, ProductExcelModel.class, params);
            List<ProductExcelModel> failList = result.getFailList();
            log.info("解析失败元素:{}",failList.size());
            List<ProductExcelModel> list = result.getList();
            log.info("预插入元素：{}",list.size());
            if(!list.isEmpty()){
                int i = productDao.addProduct(list);
                log.info("插入结果：{}",i);
            }
        }catch (Exception e){
            log.error("导入异常：{}",e.getMessage());
        }
    }

}
